package net;

/**
 * @author Ajie
 * @date 7/4/24
 * @function
 */
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.FileInputStream;
import java.io.IOException;

public class FTPUploadExample {
    public static void main(String[] args) {
//        String server = "ftp.wismed.com.cn";
        String server = "39.100.126.26";
        int port = 21;
        String user = "wismed";
        String pass = "wismed1234";
        String localFilePath = "./src/net/helloworld.txt";
//            String remoteFilePath = "remote/path/file.jar";
        String remoteFilePath = "/origin/cms/109/FDE878D15BE7/log/helloworld.txt";
//            String remoteFilePath = "/origin/cms/109/FDE878D15BE7/log/";

        FTPClient ftpClient = new FTPClient();
        FileInputStream inputStream = null;

        try {
            System.out.println("Connecting to the server...");
            ftpClient.connect(server, port);
            System.out.println("Connected to the server.");

            System.out.println("Logging in...");
            boolean login = ftpClient.login(user, pass);
            if (login) {
                System.out.println("Logged in to the server.");
            } else {
                System.out.println("Failed to log in.");
                return;
            }

            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                System.err.println("FTPUploadExample:main:code::::"+reply);
                ftpClient.disconnect();
            }else {
                System.err.println("login success reply code::::"+reply);//230

            }

            // 设置控制连接编码为UTF-8
            ftpClient.setControlEncoding("UTF-8");
//            ftpClient.sendCommand("OPTS UTF8 ON");
            // 使用主动模式
//             ftpClient.enterLocalActiveMode();
            // 使用被动模式
//            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

//            String localFilePath = "path/to/local/file.jar";
//            String remoteFilePath = "remote/path/file.jar";

            inputStream = new FileInputStream(localFilePath);
            boolean done = ftpClient.storeFile(remoteFilePath, inputStream);
            inputStream.close();

            if (done) {
                System.out.println("The file was uploaded successfully.");
            } else {
                System.out.println("Failed to upload the file.");
            }

        } catch (IOException ex) {
            System.out.println("An error occurred: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}